package coml.jiyun.hp.day01.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import java.util.ArrayList;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import coml.jiyun.hp.day01.R;
import coml.jiyun.hp.day01.bean.WanBean;

public class RlvHomeAdapter extends RecyclerView.Adapter {

    private Context context;
    private ArrayList<WanBean.DataBean.DatasBean> datasBeans;
    private int LEFT = 1;
    private int RIGHT = 2;


    public RlvHomeAdapter(Context context, ArrayList<WanBean.DataBean.DatasBean> datasBeans) {
        this.context = context;
        this.datasBeans = datasBeans;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType ==LEFT){
            View inflate = LayoutInflater.from(context).inflate(R.layout.layout_itemone, parent, false);
            return new ViewHolder1(inflate);
        }else {
            View inflate = LayoutInflater.from(context).inflate(R.layout.layout_itemtwo, parent, false);
            return new ViewHolder2(inflate);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int itemViewType = holder.getItemViewType();
        if (itemViewType == LEFT){
            ViewHolder1 holder1 = (ViewHolder1) holder;
            WanBean.DataBean.DatasBean datasBean = datasBeans.get(position);
            Glide.with(context).load(datasBean.getEnvelopePic()).apply(new RequestOptions().circleCrop()).into(holder1.mitemoneImg);
            holder1.mitemoneTv.setText(datasBean.getTitle());
        }else {
            ViewHolder2 holder2 = (ViewHolder2) holder;
            WanBean.DataBean.DatasBean datasBean = datasBeans.get(position);
            holder2.mitemtwoTv.setText(datasBean.getTitle());
            Glide.with(context).load(datasBean.getEnvelopePic()).apply(new RequestOptions().circleCrop()).into(holder2.mitemtwoImg);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position %2 == 0){
            return LEFT;
        }else{
            return RIGHT;
        }
    }

    @Override
    public int getItemCount() {
        return datasBeans.size();
    }

    public class ViewHolder1 extends RecyclerView.ViewHolder {

        ImageView mitemoneImg;
        TextView mitemoneTv;

        public ViewHolder1(@NonNull View itemView) {
            super(itemView);

            mitemoneImg = itemView.findViewById(R.id.itemone_img);
            mitemoneTv = itemView.findViewById(R.id.itemone_tv);
        }
    }

    public class ViewHolder2 extends RecyclerView.ViewHolder {

        TextView mitemtwoTv;
        ImageView mitemtwoImg;

        public ViewHolder2(@NonNull View itemView) {
            super(itemView);
            mitemtwoTv = itemView.findViewById(R.id.itemtwo_tv);
            mitemtwoImg = itemView.findViewById(R.id.itemtwo_img);
        }
    }
}